package com.ruoyi.quartz.task;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.CzEquipment;
import com.ruoyi.business.domain.CzEquipmentTrajectory;
import com.ruoyi.business.mapper.CzEquipmentMapper;
import com.ruoyi.business.mapper.CzEquipmentTrajectoryMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.sign.Md5Utils;

/**
 * 定位设备定时获取获取设备数据
 * 
 * @author ruoyi
 */
@Component("GPSTask")
public class GPSTask
{
	
	@Autowired
	private CzEquipmentMapper czEquipmentMapper;
	
	@Autowired
	private CzEquipmentTrajectoryMapper czEquipmentTrajectoryMapper;
	
    public void getGPSPostion()
    {
    	//获取GPS在线设备的定位数据
    	CzEquipment param=new CzEquipment();
    	param.setEquipmentTypeCode("GPS");
    	param.setIsOnline("online");
		List<CzEquipment> equipments=czEquipmentMapper.selectCzEquipmentList(param);
    	List<Object> postionData=getPostionData(getToken(),equipments);
    	
    	List<CzEquipment> updateList=new ArrayList<CzEquipment>();
    	
    	List<CzEquipmentTrajectory> trajectories=new ArrayList<CzEquipmentTrajectory>();
    	for(Object obj : postionData) {
    		Map<String, Object> map=JSON.parseObject(obj.toString());
    		CzEquipmentTrajectory czEquipmentTrajectory=new CzEquipmentTrajectory();
    		czEquipmentTrajectory.setEquipmentSerialNo(map.get("id").toString());
    		czEquipmentTrajectory.setTrajectoryLat(BigDecimal.valueOf(Double.valueOf(map.get("lat").toString())));
    		czEquipmentTrajectory.setTrajectoryLon(BigDecimal.valueOf(Double.valueOf(map.get("lng").toString())));
    		czEquipmentTrajectory.setCreateBy(map.get("id").toString());
    		czEquipmentTrajectory.setCreateTime(new Date());
    		trajectories.add(czEquipmentTrajectory);
    		
    		CzEquipment czEquipment=new CzEquipment();
    		czEquipment.setEquipmentSerialNo(map.get("id").toString());
    		czEquipment.setEquipmentLat(BigDecimal.valueOf(Double.valueOf(map.get("lat").toString())));
    		czEquipment.setEquipmentLon(BigDecimal.valueOf(Double.valueOf(map.get("lng").toString())));
    		updateList.add(czEquipment);
    	}
    	czEquipmentTrajectoryMapper.batchInsertCzEquipmentTrajectory(trajectories);
    	czEquipmentMapper.batchUpdateCzEquipmentBySerialNo(updateList);
    }
    
    
	
	/**
	 * 登录gis平台
	 * @return
	 */
    public Map<String, Object> getToken(){
    	//登录参数
    	Map<String, Object> loginParam=new HashMap<String, Object>();
    	loginParam.put("func", "1002");
    	loginParam.put("account", "demo");
    	loginParam.put("password", Md5Utils.hash("demo123456"));
    	loginParam.put("language", "en_US");
    	
    	String loginResult=HttpUtils.sendPost("https://api.farsp.com/api","param="+JSON.toJSONString(loginParam)+"&sign=123");
    	Map<String, Object> loginMap=JSON.parseObject(loginResult);
    	return JSON.parseObject(loginMap.get("data").toString());
    }
    
    
    /**
     * 获取定位
     * @param loginData
     * @return
     */
    public List<Object> getPostionData(Map<String, Object> loginData,List<CzEquipment> equipmentList){
    	String ids=new String();
    	for (CzEquipment czEquipment : equipmentList) {
    		ids+=(czEquipment.getEquipmentSerialNo()+",");
		}

    	//登录参数
    	Map<String, String> queryParam=new HashMap<String, String>();
    	queryParam.put("func", "1005");
    	queryParam.put("token", loginData.get("token").toString());
    	queryParam.put("car_id", ids.substring(0, ids.length()-1));
    	
    	String result=HttpUtils.sendPost("https://api.farsp.com/api","param="+JSON.toJSONString(queryParam)+"&sign=123");
    	Map<String, Object> loginMap=JSON.parseObject(result);
    	List<Object> array=JSONArray.parseArray(loginMap.get("data").toString()); 
    	return array;
    }
    
    
    /**
     * 定时获取执法记录仪的位置
     */
    public void getLawEnforcementPostion()
    {
    	//获取执法记录仪在线设备的定位数据
    	CzEquipment param=new CzEquipment();
    	param.setEquipmentTypeCode("GPS");
		List<CzEquipment> equipments=czEquipmentMapper.selectCzEquipmentList(param);
		List<Object> postionData=new ArrayList<Object>();
//    	List<Object> postionData=getPostionData(getLawEnforcementToken(),equipments);
    	
    	
    	List<CzEquipmentTrajectory> trajectories=new ArrayList<CzEquipmentTrajectory>();
    	for(Object obj : postionData) {
    		Map<String, Object> map=JSON.parseObject(obj.toString());
    		CzEquipmentTrajectory czEquipmentTrajectory=new CzEquipmentTrajectory();
    		czEquipmentTrajectory.setEquipmentSerialNo(map.get("id").toString());
    		czEquipmentTrajectory.setTrajectoryLat(BigDecimal.valueOf(Double.valueOf(map.get("lat").toString())));
    		czEquipmentTrajectory.setTrajectoryLon(BigDecimal.valueOf(Double.valueOf(map.get("lng").toString())));
    		czEquipmentTrajectory.setCreateBy(map.get("id").toString());
    		czEquipmentTrajectory.setCreateTime(new Date());
    		trajectories.add(czEquipmentTrajectory);
    	}
    	czEquipmentTrajectoryMapper.batchInsertCzEquipmentTrajectory(trajectories);
    	
    }
    public Map<String, String> getLawEnforcementToken(){
    	String loginResult=HttpUtils.sendGet("http://8.141.173.79:18080/api/user/login","username=admin&password="+Md5Utils.hash("admin"));
    	Map<String, Object> loginMap=JSON.parseObject(loginResult);
    	return null;
    }
    public static void main(String[] args) {
    	String result=HttpUtils.sendGet("http://8.141.173.79:18080/api/play/start/czbf001/czbf002");
    	Map<String, Object> Map=JSON.parseObject(result);
	}
    
}
